﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;

namespace CoolERP_Web.User
{
    public partial class CS_YSLX_Query : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.AspNetPager1.PageSize = GridView1.PageSize;
                BindData();
            }
        }
        private void BindData()
        {
            using (CS_YSLXBLL cBLL = new CS_YSLXBLL())
            {
                int RecordCount = 0;
                List<CS_YSLX> itemLists = cBLL.GetPagerList(null, "BH", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                this.AspNetPager1.RecordCount = RecordCount;
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }
        
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox mc = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMC");
            using (CS_YSLXBLL jbdwB = new CS_YSLXBLL())
            {
                CS_YSLX item = jbdwB.GetEntity(t => t.BH == GridView1.DataKeys[e.RowIndex].Value.ToString());
                item.MC = mc.Text.ToString().Trim();
                if (jbdwB.Update(item))
                    this.ShowMsg("更新成功");
                else
                    this.ShowMsg("更新失败");
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            List<CS_YSLX> items = new List<CS_YSLX>();
            CS_YSLX item = new CS_YSLX();
            try
            {
                using (CS_YSLXBLL cBLL = new CS_YSLXBLL())
                {
                    foreach (GridViewRow container in GridView1.Rows)
                    {
                        HtmlInputCheckBox cBox = (HtmlInputCheckBox)container.FindControl("chkSel");
                        if (cBox != null && cBox.Checked)
                        {
                            item = cBLL.GetEntity(t => t.BH == cBox.Value);
                            items.Add(item);
                        }
                    }
                    if (cBLL.Deletes(items))
                        ShowMsg("删除成功");
                }
            }
            catch (Exception ex)
            {
                this.ShowMsg("删除失败\n" + ex.Message);

            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = true;
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            int result = 0;
            TextBox txtBH = GridView1.FooterRow.FindControl("txtBH") as TextBox;
            TextBox txtMC = GridView1.FooterRow.FindControl("txtMC") as TextBox;
            using (CS_YSLXBLL cBLL = new CS_YSLXBLL())
            {
                CS_YSLX item = new CS_YSLX();
                item.BH = txtBH.Text.Trim();
                item.MC = txtMC.Text.Trim();
                result = cBLL.SaveEntity(item, t => t.BH == item.BH && t.MC == item.MC);
            }
            if (result == 1)
                ShowMsg("保存成功");
            else if (result == -1)
                ShowMsg("已存在此类型，请重新输入");
            else
                ShowMsg("保存失败");
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindData();
        }
    }
}